package com.itheima.quartz;

import com.itheima.arithmetic.ReducePriceUtils;
import com.itheima.dao.MoneyPackageDao;
import com.itheima.domain.MoneyPackage;
import com.itheima.util.Constant;
import com.itheima.util.RateRandomNumber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @program: cache
 * @description: 定时加载红包
 * @author: zhanghz001
 * @create: 2021-04-27 11:10
 **/
@Component
public class ZhzMoneyPushTask {
    @Autowired
    private MoneyPackageDao moneyPackageDao;
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    @Scheduled(cron = "0/30 * * * * ?")
    public void pushMoney() {
        //获取一个红包
        MoneyPackage one = moneyPackageDao.findOne();
        if (one != null) {
            //修改状态到3
            moneyPackageDao.modify(one.getId());
            //生成多个红包
            List<Double> doubles = ReducePriceUtils.splitReducePrice(one.getMoney(), one.getCount());
            Double[] money = new Double[doubles.size()];
            doubles.toArray(money);
            
            //写入到redis中去
            redisTemplate.boundListOps(Constant.MONEY_QUEUE_NAME).leftPushAll(money);
        }
    }
}
